package com.situ.hotel.controller;

//import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.situ.hotel.entity.Order;
import com.situ.hotel.entity.Result;
import com.situ.hotel.entity.Room;
import com.situ.hotel.service.RoomService;

@RestController // 返回json数据
@RequestMapping("/api/room") // 所有URL方法
public class RoomController {

	@Resource
	private RoomService roomService;

	@PostMapping("/add")
	public Result add(Room room) {

		// 1.注入参数
		// 2.业务逻辑
		// System.out.println("11");

		int code = roomService.add(room);
		System.out.println(code);
		if (code > 0) {
			return Result.success(code, null);
		} else if (code == -1) {
			return Result.error(code, "房间号不能为空，也不能超过八个字符");
		} else if (code == -2) {
			return Result.error(code, "房间类型不正确");
		} else if (code == -3) {
			return Result.error(code, "描述不能超过128个字节");
		} else if (code == -4) {
			return Result.error(code, "房间价位应为正数");
		} else if (code == -5) {
			return Result.error(code, "房间号已存在");
		} else {
			return Result.error(code, "未知错误，联系技术人员");
		}
	}

	@GetMapping("/search")
	public Result search(Integer page, Integer limit, Room room) {
		PageInfo pageInfo = roomService.search(page, limit, room);
		return Result.success(0, pageInfo);
	}

//    public Result query(Integer page,) {
//    	
//    }

	@GetMapping({"/query","/user/query"})
	public Result query(Integer page, Integer limit, Room room, Order order) {
		PageInfo pageInfo = roomService.search(page, limit, room, order);
		return Result.success(0, pageInfo);
	}

	@PostMapping("/edit")
	public Result edit(Room room) {
		int code = roomService.edit(room);
		if (code > 0) {
			return Result.success(code, null);
		} else if (code == -1) {
			return Result.error(code, "ID不存在");
		} else if (code == -2) {
			return Result.error(code, "房间名重复");
		} else {
			return Result.error(code, "未知错误");
		}

	}

	@GetMapping("/remove")
	public Result remove(int id) {
		int code = roomService.remove(id);
		if (code > 0) {
			return Result.success(code, null);
		} else {
			return Result.error(code, "未知错误");
		}
	}
}
